/**
* Created by wl
* 项目管理>持有中-详情页-投资减持信息
*/
<template>
    <div>
        <div class="ca-box ca-box-border-top-none ca-p-20" v-if="!showEdit">
            <div class="ca-title">
                <h3>初始投资</h3>
                <el-button type="text" class="ca-edit-btn el-icon-edit ca-m-r-10 edit-btn" @click="editInfo" :disabled="!isAuthIndexSee">编辑</el-button>
            </div>
            <table class="ca-table ca-m-t-15" id="initialBox">
                <tr>
                    <th width="15%">投资基金</th>
                    <th width="10%">投资时间</th>
                    <th width="12%">初始投资总额<br />(万元)</th>
                    <th width="12%">初始持股数量<br />(万股)</th>
                    <th width="12%">初始投资成本<br />(元/股)</th>
                    <th width="12%">项目初始总股本</th>
                    <th width="12%">初始持股比例</th>
                    <th >投后项目估值<br />(万元)</th>
                </tr>
                <tr v-for="(item, index) in itemInvestInits" :key="index">
                    <td><el-button type="text" @click="goIndex(item.fundId)">{{item.fundName}}</el-button></td>
                    <td>{{$moment(item.investDate).format('YYYY-MM-DD')}}</td>
                    <td>{{item.investAmount | format1}}</td>
                    <td>{{item.finalHolding | format1}}</td>
                    <td>{{item.investCost | format1}}</td>
                    <td>{{item.totalCapital}}</td>
                    <td>{{item.holdScale*100 | format1}}%</td>
                    <td>{{item.valuationAfter | format1}}</td>
                </tr>
            </table>
            <div class="ca-title ca-m-t-15">
                <h3>当前持股情况</h3>
                <div class="clear"></div>
            </div>
            <table class="ca-table ca-m-t-15" id="currentBox">
                <tr>
                    <th width="15%">投资基金</th>
                    <th width="10%">投资时间</th>
                    <th width="10%">当前投资金额<br />(万元)</th>
                    <th width="10%">当前持股数量<br />(万股)</th>
                    <th width="10%">当前投资成本<br />(元/股)</th>
                    <th width="10%">当前项目总股本<br />(万股)</th>
                    <th width="10%">当前持股比例</th>
                    <th width="10%">当前持有估值<br />(万元)</th>
                    <th>投后项目估值<br />(万元)</th>
                </tr>
                <tr v-for="(item, index) in itemInvestCurrents" :key="index">
                    <td><el-button type="text" @click="goIndex(item.fundId)">{{item.fundName}}</el-button></td>
                    <td>{{$moment(item.investDate).format('YYYY-MM-DD')}}</td>
                    <td>{{item.investAmount | format1}}</td>
                    <td>{{item.finalHolding | format1}}</td>
                    <td>{{item.investCost | format1}}</td>
                    <td>{{item.totalCapital}}</td>
                    <td>{{item.holdScale*100 | format1}}%</td>
                    <td>{{item.valuationHold | format1}}</td>
                    <td>{{item.valuationAfter | format1}}</td>
                </tr>
            </table>
            <div class="ca-title ca-m-t-15">
                <h3>历史变动</h3>
                <div class="ca-pull-right">
                    <el-select v-model="changeTypeList" placeholder="请选择" class="ca-wid" @change="filterChangeType(changeTypeList)">
                        <el-option label="全部" value=''></el-option>
                        <el-option
                                v-for="item in dropDown.investChangeType"
                                :label="item.name"
                                :value="item.id"
                                :key="item.id">
                        </el-option>
                    </el-select>
                    <el-select v-model="fundIdList" placeholder="请选择" class="ca-m-l-10 ca-wid" @change="filterFundId(fundIdList)">
                        <el-option label="全部" value=""></el-option>
                        <el-option
                                v-for="(item, index) in fundNameList"
                                :label="item.fundName"
                                :value="item.fundId"
                                :key="index">
                        </el-option>
                    </el-select>
                    <el-button type="primary" @click="fileDownLoad" :disabled="!isAuthIndexSee">下载</el-button>
                </div>
            </div>
            <table class="ca-table ca-m-t-15" id="historyBox">
                <tr>
                    <th width="12%" class="b_l">投资基金</th>
                    <th width="8%">变动类型</th>
                    <th width="7%">投资类型</th>
                    <th width="8%">变动时间</th>
                    <th width="8%">期初持股数<br />(万股)</th>
                    <th width="8%">变化股数<br />(万股)</th>
                    <th width="7%">期末持股<br />(万股)</th>
                    <th width="7%">变动金额<br />(万元)</th>
                    <th width="8%">变动价格<br />(元/股)</th>
                    <th width="8%">当前成本<br />(元/股)</th>
                    <th width="8%">项目总股本<br />(万股)</th>
                    <th width="7%">项目估值<br />(万元)</th>
                    <th width="7%">退出倍数</th>
                    <th class="b_r">IRR</th>
                </tr>
                <tr v-for="(item, index) in itemHoldInvestEntities" :key="index">
                    <td  class="b_l"><el-button type="text" @click="goIndex(item.fundId)">{{item.fundName}}</el-button></td>
                    <td>{{item.changedType | getNameById('investChangeType')}}</td>
                    <td v-if="item.changedType == 2 || item.changedType == 4">{{item.investType | getNameById('itemExit')}}</td>
                    <td v-else>{{item.investType | getNameById('investType')}}</td>
                    <td>{{$moment(item.investDate).format('YYYY-MM-DD')}}</td>
                    <td>{{item.beginHolding | format1}}</td>
                    <td>{{item.investNumber | format1}}</td>
                    <td>{{item.finalHolding | format1}}</td>
                    <td>{{item.investLoan | format1}}</td>
                    <td>{{item.changeFold | format1}}</td>
                    <td>{{item.currentCost | format1}}</td>
                    <td>{{item.totalCapital}}</td>
                    <td>{{item.valuationAfter | format1}}</td>
                    <td>{{item.exitMultiple}}</td>
                    <td width="8%">{{item.irr | format1}}%</td>
                </tr>
            </table>
        </div>
        <csInvestmentReduction v-else @close="close" :itemHoldInvestEntities = 'itemHoldInvestEntities' @updateList='updateList' @save="save"></csInvestmentReduction>
    </div>
</template>
<script type="text/ecmascript-6">
    import {api} from '../api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    import { addRemind, isNoAuth } from '__service/utils';
    export default {
        data() {
            return {
                isAuthIndexSee: isNoAuth('持有中项目-投资减持-编辑'),
                dialogVisible: false,
                showEdit: false,
                itemInvestInits: [], //初始投资
                itemInvestCurrents: [], //当前持股情况
                itemHoldInvestEntities: [], //历史变动数据
                changeTypeList: '', //历史变动类型筛选
                fundIdList: '', //历史基金筛选
                fundNameList: [], //基金筛选列表
                dropDown: {
                    investChangeType: this.$store.state.user.dictionaryMap.investChangeType, //变动类型
                },
            }
        },
        watch: {
        },
        props: {
        },
        mounted() {
            this.getList()
            this.init()
            this.getFundList()
        },
        methods: {
            editInfo() {
                this.showEdit = true
            },
            close() {
                this.showEdit = false
                this.init()
            },
            goIndex(id) {
                this.$router.push({name: 'invested-fund', params: {fundId: id, tabIndex: 0, openWindow: true }});
            },
            async save() {
                await this.updateList()
                await this.checkHistory()
                // 刷新头部信息
                this.$emit('updateHeaderInfo')
                // 添加小红点
                await addRemind({
                    dataId: this.$route.params.itemId, // 业务id
                    dataTab: 2, // tab页顺序 head 也为0 新建为'-1'
                    dataType: 2, // 1拟投资项目,2已投资项目,3母基金,4拟设立基金,5已设立基金,6客户,7潜在项目
                });
                // 根据第三方组件来调用 父组件中定义的方法 重新获取小红点
                this.$root.Dot.$emit('getDotList');
                // 添加小红点 end
            },
            init() {
                this.checkInit()
                this.checkCurrent()
                this.checkHistory()
            },
            filterFundId(fundId) {
                this.fundIdList = fundId
                let query = {
                    itemId: this.$route.params.itemId,
                    fundId: fundId,
                    changedType: this.changeTypeList, //变动类型
                    investType: null, //投资类型
                }
                apiHttp(api.CHECK_HISTORY, query).then(ret=> {
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.itemHoldInvestEntities = ret.data.itemHoldInvestEntities
                    }
                })
            },
            filterChangeType(changedType) {
                this.changeTypeList = changedType
                let query = {
                    itemId: this.$route.params.itemId,
                    fundId: this.fundIdList,
                    changedType: changedType, //变动类型
                    investType: null, //投资类型
                }
                apiHttp(api.CHECK_HISTORY, query).then(ret=> {
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.itemHoldInvestEntities = ret.data.itemHoldInvestEntities
                    }
                })
            },
            //更新列表
            updateList() {
                this.init()
                this.getFundList()
            },
            // 获取已设立基金列表
            getList() {
                apiHttp(api.GET_LIST).then(ret=> {
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.fundList = ret.data || []
                    }
                })
            },
            // 查看初始投资
            checkInit() {
                apiHttp(api.CHECK_INIT, {itemId: this.$route.params.itemId }).then(ret=> {
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.itemInvestInits = ret.data.itemInvestInits
                    }
                })
            },
            // 查看当前持股情况
            checkCurrent() {
                apiHttp(api.CHECK_CURRENT, {itemId: this.$route.params.itemId }).then(ret=> {
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.itemInvestCurrents = ret.data.itemInvestCurrents
                    }
                })
            },
            // 查看历史变动数据
            checkHistory() {
                let query = {
                    itemId: this.$route.params.itemId,
                    fundId: '',
                    changedType: '', //变动类型
                    investType: null, //投资类型
                }
                apiHttp(api.CHECK_HISTORY, query).then(ret=> {
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.itemHoldInvestEntities = ret.data.itemHoldInvestEntities
                    }
                })
            },
            // 基金筛选列表
            getFundList() {
                let query = {
                    itemId: this.$route.params.itemId,
                    fundId: '',
                    changedType: '', //变动类型
                    investType: null, //投资类型
                }
                apiHttp(api.CHECK_HISTORY, query).then(ret=> {
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.fundNameList = []
                        this.itemHoldInvestEntities = ret.data.itemHoldInvestEntities
                        if (this.itemHoldInvestEntities.length > 0) {
                            for (var i = 0; i < this.itemHoldInvestEntities.length; i++) {
                                var flag = true
                                for (var j = 0; j < this.fundNameList.length; j++) {
                                    if (this.itemHoldInvestEntities[i].fundId === this.fundNameList[j].fundId || this.itemHoldInvestEntities[i].fundId === '-1') {
                                        flag = false
                                    }
                                }
                                if (flag) {
                                    this.fundNameList.push(this.itemHoldInvestEntities[i])
                                }
                            }
                        }
                    }
                })
            },
            //文件下载
            fileDownLoad() {
                 if (this.itemHoldInvestEntities.length === 0) {
                     this.$message({
                         type: 'warning',
                         message: '无数据!'
                     });
                 } else {
                     window.open(this.baseURL + api.EXPORT_FILE.url + '?X-Token=' + this.$store.state.user.token + '&type=22&changedType=' + this.changeTypeList + '&fundId=' + this.fundIdList + '&itemId=' + this.$route.params.itemId);
                 }
            },
        },
        components: {
            csInvestmentReduction: require('./edit')
        },
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.edit-btn {
    float: right;
    cursor: pointer;
}
.ca-table th {
    line-height: 20px;
}
</style>
